# import logging

from Case.python_ui.Common.basepage import Basepage
import time,pytest

from Case.python_ui.PageLocate.admin_all_notification_locator import AdminAllNotificationLocator
from Case.python_ui.PageLocate.admin_notification_record_locator import AdminNotificationRecordLocator
from Case.python_ui.Pageobject.admin_all_notification_pages import AdminAllNotificationPage
from Case.python_ui.Test_data.login_data import Logindata


class AdminNotificationRecordPage(Basepage):


    #进入new ticket菜单页面
    def enter_notification_record_page(self):
        time.sleep(2)
        doc = "进入notification record菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminNotificationRecordLocator.notification_record_tab,doc)
                self.click_element(AdminNotificationRecordLocator.notification_record_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    #检查record页面
    def check_record_page(self):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "检查record页面"
        self.wait_elevisble(AdminNotificationRecordLocator.notification_title,doc)
        title = self.get_element(AdminNotificationRecordLocator.notification_title,doc).text
        assert title == "Notification Title"
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_table, doc)
        title = self.get_element(AdminNotificationRecordLocator.account_type_table, doc).text
        assert title == "Account Type"
        self.wait_elevisble(AdminNotificationRecordLocator.account_email, doc)
        title = self.get_element(AdminNotificationRecordLocator.account_email, doc).text
        assert title == "Account Email"
        self.wait_elevisble(AdminNotificationRecordLocator.notification_type, doc)
        title = self.get_element(AdminNotificationRecordLocator.notification_type, doc).text
        assert title == "Notification Type"
        self.wait_elevisble(AdminNotificationRecordLocator.channel, doc)
        title = self.get_element(AdminNotificationRecordLocator.channel, doc).text
        assert title == "Channel"
        self.wait_elevisble(AdminNotificationRecordLocator.send_time, doc)
        title = self.get_element(AdminNotificationRecordLocator.send_time, doc).text
        assert title == "Send Time (UTC+08:00)"
        self.wait_elevisble(AdminNotificationRecordLocator.send_statis, doc)
        title = self.get_element(AdminNotificationRecordLocator.send_statis, doc).text
        assert title == "Send Status"
        self.wait_elevisble(AdminNotificationRecordLocator.read_status, doc)
        title = self.get_element(AdminNotificationRecordLocator.read_status, doc).text
        assert title == "Read Status"

    #获取用户端账号
    def get_user_email(self,cmdopt):
        email = Logindata.user_data[cmdopt]['user']['username']
        return email

    # 获取管理员端账号
    def get_admin_email(self,cmdopt):
        email = Logindata.user_data[cmdopt]['admin']['username']
        return email

    #Search功能验证
    def search_record(self,cmdopt):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "Search功能验证"
        user_email = self.get_user_email(cmdopt)
        #按account type搜索
        self.wait_elevisble(AdminNotificationRecordLocator.account_type,doc)
        self.click_element(AdminNotificationRecordLocator.account_type,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value,doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords,doc)
        self.send_keys(AdminNotificationRecordLocator.search_keywords,user_email,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button,doc)
        self.click_element(AdminNotificationRecordLocator.search_button,doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_email,doc)
        email = self.get_element(AdminNotificationRecordLocator.search_record_email,doc).text
        assert email == user_email
        self.click_element(AdminNotificationRecordLocator.reset_button,doc)
        #按notification type搜索
        self.wait_elevisble(AdminNotificationRecordLocator.search_type,doc)
        self.click_element(AdminNotificationRecordLocator.search_type,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_type_value, doc)
        self.click_element(AdminNotificationRecordLocator.search_type_value, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_notification_type,doc)
        self.click_element(AdminNotificationRecordLocator.search_notification_type,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.notification_type_value, doc)
        self.click_element(AdminNotificationRecordLocator.notification_type_value, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords_template, doc)
        self.send_keys(AdminNotificationRecordLocator.search_keywords_template,'Activation successful notification',doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_title, doc)
        title = self.get_element(AdminNotificationRecordLocator.search_record_title, doc).text
        title = title.split(" ")
        assert "Welcome" == title[0]


    def refresh_record(self,cmdopt):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "重置record页面数据"
        user_email = self.get_user_email(cmdopt)
        # 按account type搜索
        self.wait_elevisble(AdminNotificationRecordLocator.account_type, doc)
        self.click_element(AdminNotificationRecordLocator.account_type, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value, doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords, doc)
        self.send_keys(AdminNotificationRecordLocator.search_keywords, user_email, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_email, doc)
        email = self.get_element(AdminNotificationRecordLocator.search_record_email, doc).text
        assert email == user_email
        self.click_element(AdminNotificationRecordLocator.reset_button, doc)
        key_words = self.get_element(AdminNotificationRecordLocator.search_keywords,doc).text
        assert key_words == ""
        self.wait_elevisble(AdminNotificationRecordLocator.empty_data,doc)
        empty = self.get_element(AdminNotificationRecordLocator.empty_data,doc).text
        assert empty == "No Data"

    def view_record_detail(self,cmdopt):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "查看records详情"
        # 按account type搜索
        user_email = self.get_user_email(cmdopt)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type, doc)
        self.click_element(AdminNotificationRecordLocator.account_type, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value, doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords, doc)
        self.send_keys(AdminNotificationRecordLocator.search_keywords, user_email, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_email, doc)
        email = self.get_element(AdminNotificationRecordLocator.search_record_email, doc).text
        assert email == user_email
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_title_button,doc)
        title = self.get_element(AdminNotificationRecordLocator.search_record_title,doc).text
        self.click_element(AdminNotificationRecordLocator.search_record_title_button,doc)
        time.sleep(1)
        self.wait_elevisble(AdminNotificationRecordLocator.detail_title,doc)
        detail_title = self.get_element(AdminNotificationRecordLocator.detail_title,doc).text
        assert title == detail_title

    def view_record_list_data(self,cmdopt):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "records列表数据显示"
        # 按account type搜索
        self.wait_elevisble(AdminNotificationRecordLocator.account_type, doc)
        self.click_element(AdminNotificationRecordLocator.account_type, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords, doc)
        admin_email = self.get_admin_email(cmdopt)
        self.send_keys(AdminNotificationRecordLocator.search_keywords, admin_email, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.total_count,doc)
        total = self.get_element(AdminNotificationRecordLocator.total_count,doc).text
        doc = '到all notification页面删掉记录'
        self.wait_elevisble(AdminAllNotificationLocator.all_notification_tab, doc)
        self.click_element(AdminAllNotificationLocator.all_notification_tab, doc)
        AdminAllNotificationPage(self.driver).delete_notification_in_list()
        self.enter_notification_record_page()
        # 按account type搜索
        self.wait_elevisble(AdminNotificationRecordLocator.account_type, doc)
        self.click_element(AdminNotificationRecordLocator.account_type, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords, doc)
        admin_email = self.get_admin_email(cmdopt)
        self.send_keys(AdminNotificationRecordLocator.search_keywords, admin_email, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        self.wait_elevisble(AdminNotificationRecordLocator.total_count, doc)
        new_total_cost = self.get_element(AdminNotificationRecordLocator.total_count,doc).text
        # logging.info(">>>>>>>>>>>>>>>>>total:",total)
        # logging.info(">>>>>>>>>>>>>>>>>new_total_cost:", new_total_cost)
        assert total == new_total_cost


    def flip_page_view_record_detail(self,cmdopt):
        self.enter_notification_record_page()
        time.sleep(2)
        doc = "admin端的record界面的SMS的翻页按钮校验"
        self.wait_elevisble(AdminNotificationRecordLocator.account_type, doc)
        self.click_element(AdminNotificationRecordLocator.account_type, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.click_element(AdminNotificationRecordLocator.account_type_value_admin, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_keywords, doc)
        admin_email = self.get_admin_email(cmdopt)
        self.send_keys(AdminNotificationRecordLocator.search_keywords, admin_email, doc)
        self.wait_elevisble(AdminNotificationRecordLocator.search_button, doc)
        self.click_element(AdminNotificationRecordLocator.search_button, doc)
        time.sleep(4)
        #记录第一条消息titile
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_title,doc)
        first_title = self.get_element(AdminNotificationRecordLocator.search_record_title,doc).text
        #记录第二条消息的titile
        self.wait_elevisble(AdminNotificationRecordLocator.search_record_title_second,doc)
        second_title = self.get_element(AdminNotificationRecordLocator.search_record_title_second,doc).text
        #查看第一条消息详情页
        self.click_element(AdminNotificationRecordLocator.search_record_title_button,doc)
        time.sleep(1)
        self.wait_elevisble(AdminNotificationRecordLocator.detail_title,doc)
        self.wait_elevisble(AdminNotificationRecordLocator.next_button,doc)
        self.click_element(AdminNotificationRecordLocator.next_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminNotificationRecordLocator.detail_title,doc)
        check_title = self.get_element(AdminNotificationRecordLocator.detail_title,doc).text
        assert check_title == second_title
        self.wait_elevisble(AdminNotificationRecordLocator.previous_button,doc)
        self.click_element(AdminNotificationRecordLocator.previous_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminNotificationRecordLocator.detail_title, doc)
        check_title = self.get_element(AdminNotificationRecordLocator.detail_title, doc).text
        assert check_title == first_title





